package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.LeaderProject;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.DraftAchievement;
import com.cls.business.service.IDraftAchievementService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 拟定的研究成果 Controller
 *
 * @author slx
 * @date 2020-12-02 14:26:28
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class DraftAchievementController extends BaseController {

    private final IDraftAchievementService draftAchievementService;

    /**
    * 拟定的研究成果列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/draftAchievement")
    public String draftAchievementIndex(){
        return MyUtil.view("business/draftAchievement/draftAchievement");
    }

    /**
    * 拟定的研究成果新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/draftAchievement/draftAchievementAdd")
    public String draftAchievementAdd(Model model){
        return MyUtil.view("business/draftAchievement/draftAchievementAdd");
    }

    /**
    * 拟定的研究成果修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/draftAchievement/draftAchievementUpdate/{draftAchievementId}")
    public String draftAchievementUpdate(@PathVariable Long draftAchievementId, Model model){
        DraftAchievement draftAchievement = draftAchievementService.getById(draftAchievementId);
        model.addAttribute("draftAchievement",draftAchievement);
        return MyUtil.view("business/draftAchievement/draftAchievementUpdate");
    }

    @GetMapping("{declareId}/draftAchievement/list")
    @ResponseBody
    public ResponseVO leaderProjectList(QueryRequest request, @PathVariable long declareId) {
        DraftAchievement draftAchievement=new DraftAchievement();
        draftAchievement.setDeclareId(declareId);
        PageInfo<DraftAchievement> pageInfo = draftAchievementService.findDraftAchievements(request, draftAchievement);
        return ResponseVO.success(pageInfo);
    }
    @GetMapping("{declareId}/draftAchievement/listAll")
    @ResponseBody
    public ResponseVO leaderProjectLists(@PathVariable long declareId) {
        DraftAchievement draftAchievement=new DraftAchievement();
        draftAchievement.setDeclareId(declareId);
        List<DraftAchievement> list = draftAchievementService.findDraftAchievements(draftAchievement);
        return ResponseVO.success(list);
    }

    /**
    * 拟定的研究成果详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/draftAchievement/draftAchievementDetail/{draftAchievementId}")
    public String draftAchievementDetail(@PathVariable Long draftAchievementId, Model model){
        DraftAchievement draftAchievement = draftAchievementService.getById(draftAchievementId);
        model.addAttribute("draftAchievement",draftAchievement);
        return MyUtil.view("business/draftAchievement/draftAchievementDetail");
    }

    @GetMapping("draftAchievement")
    @ResponseBody
    @RequiresPermissions("draftAchievement:list")
    public ResponseVO getAllDraftAchievements(DraftAchievement draftAchievement) {
        return ResponseVO.success(draftAchievementService.findDraftAchievements(draftAchievement));
    }

    @GetMapping("draftAchievement/list")
    @ResponseBody
    public ResponseVO draftAchievementList(QueryRequest request, DraftAchievement draftAchievement) {
        PageInfo<DraftAchievement> pageInfo =  this.draftAchievementService.findDraftAchievements(request, draftAchievement);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增DraftAchievement", exceptionMessage = "新增DraftAchievement失败")
    @PostMapping("draftAchievement")
    @ResponseBody
    @RequiresPermissions("draftAchievement:add")
    public ResponseVO addDraftAchievement(@Valid DraftAchievement draftAchievement) {
        this.draftAchievementService.createDraftAchievement(draftAchievement);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除DraftAchievement", exceptionMessage = "删除DraftAchievement失败")
    @PostMapping("draftAchievement/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("draftAchievement:delete")
    public ResponseVO deleteDraftAchievements(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.draftAchievementService.deleteDraftAchievements(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个DraftAchievement", exceptionMessage = "删除DraftAchievement失败")
    @PostMapping("draftAchievement/delete/{draftAchievementId}")
    @ResponseBody
    @RequiresPermissions("draftAchievement:delete")
    public ResponseVO deleteDraftAchievement(@PathVariable Long draftAchievementId) {
        this.draftAchievementService.removeById(draftAchievementId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改DraftAchievement", exceptionMessage = "修改DraftAchievement失败")
    @PostMapping("draftAchievement/update")
    @ResponseBody
    @RequiresPermissions("draftAchievement:update")
    public ResponseVO updateDraftAchievement(DraftAchievement draftAchievement) {
        this.draftAchievementService.updateDraftAchievement(draftAchievement);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改DraftAchievement", exceptionMessage = "导出Excel失败")
    @PostMapping("draftAchievement/excel")
    @ResponseBody
    @RequiresPermissions("draftAchievement:export")
    public void export(QueryRequest queryRequest, DraftAchievement draftAchievement, HttpServletResponse response) {
        List<DraftAchievement> draftAchievements = this.draftAchievementService.findDraftAchievements(queryRequest, draftAchievement).getList();
        ExcelKit.$Export(DraftAchievement.class, response).downXlsx(draftAchievements, false);
    }
}
